<template>
 <div class="goods">
   <!-- 添加用户按钮 -->
    <el-button class="addBtn" type="success" @click='$router.push("/goods-add")'>添加商品</el-button>
   <!-- 表格 -->
    <el-table :data="goodsList">
      <el-table-column type="index" :index='indexMethod' label="#" width="50%"></el-table-column>
      <el-table-column label="商品名称" prop="goods_name"></el-table-column>
      <el-table-column label="商品价格" prop="goods_price"></el-table-column>
      <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
      <el-table-column label="创建时间" prop="add_time"></el-table-column>
      <el-table-column label="操作">
        <template v-slot:default="{ row }">
          <el-button plain size="small"  type="primary" icon="el-icon-edit"></el-button>
          <el-button plain size="small" type="danger" icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
 </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      goodsList: [],
      pagenum: 1,
      pagesize: 5,
      total: 9999
    }
  },
  methods: {
    async getGoodsList () {
      const { meta, data } = await this.$axios.get('goods', {
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      if (meta.status === 200) {
        console.log(data)
        data.goods.forEach(item => {
          item.add_time = moment().format('YYYY-MM-DD HH:mm:ss')
        })
        this.goodsList = data.goods
        this.total = data.total
      } else {
        console.log(meta.msg)
      }
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.pagenum = 1
      this.getGoodsList()
    },
    handleCurrentChange (val) {
      this.pagenum = val
      this.getGoodsList()
    },
    indexMethod (index) {
      return index + (this.pagenum - 1) * this.pagesize + 1
    }
  },
  created () {
    this.getGoodsList()
  }
}
</script>

<style lang='scss' scoped>
  .addBtn {
    margin-bottom: 20px
  }
</style>
